#include "ReadFile.h"

int idx;
static FILE * filegue;
char * s;
boolean EOL;
boolean EOf;

void ignoreBlank() {
    while((s[idx] == ' ') || (s[idx] == '\t')) {
        idx++;
    }
}

myKata getKata () {
    myKata temp;
    createEmpty(&temp);
    int i = 0;
    ignoreBlank();
    if ((s[idx] != '\0') && (s[idx] != '\n')) {
        while ((s[idx] != ' ') && (s[idx] != '\t') && (s[idx] != '\0') && (s[idx] != '\n')) {
            temp.kata[i] = s[idx];
            temp.length = temp.length + 1;
            i++;   
            idx++;
            if ((s[idx] == '\0') || (s[idx] == '\n')) {
                EOL = true;
            }
        }
    } else {
        EOL = true;
    }
    return temp;
}

void openFile (myKata temp) {
    filegue = fopen(temp.kata,"r");
    EOf = false;
    s = (char *) malloc (101);
}

void readLine () {
    int byte_read;
    int i;
    int nBytes = 100;
    
    for (i = 0; i < nBytes + 1; i++) {
        s[i] = '\0';
    }
    byte_read = fgets(s,nBytes,filegue);
    if (byte_read == 0) {
        EOL = true;
        EOf = true;
    } else {
        idx = 0;
        EOL = false;
    }
}

void closeFile () {
    fclose(filegue);
}